package com.sllp.C71S3TsllpShop.biz.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.sllp.C71S3TsllpShop.bean.Business;
import com.sllp.C71S3TsllpShop.bean.BusinessExample;
import com.sllp.C71S3TsllpShop.bean.BusinessExample.Criteria;
import com.sllp.C71S3TsllpShop.bean.ChartData;
import com.sllp.C71S3TsllpShop.bean.OrdersItem;
import com.sllp.C71S3TsllpShop.bean.Product;
import com.sllp.C71S3TsllpShop.bean.Result;
import com.sllp.C71S3TsllpShop.bean.Specs;
import com.sllp.C71S3TsllpShop.bean.User;
import com.sllp.C71S3TsllpShop.biz.BusinessBiz;
import com.sllp.C71S3TsllpShop.dao.BusinessMapper;
import com.sllp.C71S3TsllpShop.dao.UserMapper;
import com.sllp.C71S3TsllpShop.util.GenerateOrdersItemChartData;

@Service
public class BusinessBizImpl implements BusinessBiz {

	@Resource
	private UserBizImpl ub;
	@Resource
	private BusinessMapper bm;
	@Resource
	private GenerateOrdersItemChartData generateOrdersItemChartData;
	
	@Resource
	private UserMapper um;

	@Override
	public List<Business> selectByPrimaryKey(int id) {
		BusinessExample example = new BusinessExample();
		example.createCriteria().andIdEqualTo(id);
		List<Business> list = bm.selectByExampleWithBLOBs(example);
		return list;
	}

	@Override
	public Result businessLogin(User user) {
		Result result = ub.userLogin(user);
		if(result.getSucess()==0) {
			return result;
		}
		User loginUser = (User) result.getObj();
		BusinessExample businessExample = new BusinessExample();
		businessExample.createCriteria().andIdEqualTo(loginUser.getId());
		List<Business> businesss = bm.selectByExampleWithBLOBs(businessExample);
		if(businesss.size()>1) {
			// 用户存在重复，要求管理员处理，此处可发送邮件给数据库管理员处理这个情况
			return new Result(0, "该用户存在多个商铺,已禁止登录");
		}else if(businesss.size()==0) {
			return new Result(0,"该用户没有注册成为商铺");
		}
		Business business = businesss.get(0);
		loginUser.setLoginState(1);
		int updateResult = bm.updateByPrimaryKey(business);
		if(updateResult<0) {
			return new Result(0,"登陆失败");
		}
		List<Object> login = new ArrayList<Object>();
		login.add(business);
		login.add(loginUser);
		return new Result(1,"登陆成功",login);
	}
	
	@Override
	public Result selectBusinessToSendEmailByLoginName(String loginName) {
		Result result = ub.findUserEmail(loginName);
		if(result.getSucess()==0) {
			return result;
		}
		User user = (User)result.getObj();
		BusinessExample businessExample = new BusinessExample();
		businessExample.createCriteria().andUserEqualTo(user.getId());
		List<Business> business = bm.selectByExample(businessExample);
		if(business.size() == 1) {
			return new Result(1,user);
		}
		return new Result(0, "该用户没有注册成为商家");
	}
	
	@Override
	public List<ChartData> generateIndexChartData(Business business) {
		// 获取属于这个商家的所有订单
		List<OrdersItem> ordersItems = new ArrayList<OrdersItem>();
		for(Product product : business.getProducts()) {
			for(Specs specs : product.getSpecs()) {
				if(specs.getOrdersItems().size()>0) {
					ordersItems.addAll(specs.getOrdersItems());
				}
			}
		}
		
		return generateOrdersItemChartData.generateOrdersItemChartData(ordersItems);
	}
	
	
	//分页显示和商家查询
	@Override
	public List<Business> findBusinessByCondition(Business business, Integer page, Integer pageSize){
		BusinessExample be=new BusinessExample();
		Criteria criteria=be.createCriteria();
		if(business!=null) {
			if(business.getId()!=null) {
				criteria.andIdEqualTo(business.getId());
			}
			if(business.getName()!=null) {
				criteria.andNameLike("%"+business.getName()+"%");
			}
			if(business.getState()!=null) {
				criteria.andStateEqualTo(business.getState());
			}
		}
		List<Business> businesslist = null;
		if(page!=null) {
			PageHelper.startPage(page, pageSize);
			businesslist = bm.selectByExampleWithBLOBsBusiness(be);
		}else {
			businesslist = bm.selectByExampleWithBLOBsBusiness(be);
		}
		return businesslist;
	}
	
	@Override
	public Result updateBusinessState(Business business){
		if(business.getState()==1){
			business.setState(3);
		}else if(business.getState()==3){
			business.setState(4);
		}else if(business.getState()==4){
			business.setState(3);
		}
		int code=bm.updateByPrimaryKeySelective(business);
		if(code>0) {
			return new Result(1, "修改成功");
		}
		return new Result(0, "修改失败");
	}
	
    public Result businessRegist(Business business){
		int result=bm.insert(business);
		if(result>0) {
			return new Result(1, "注册成功");
		}
		return new Result(0, "注册失败");
	}
}
